package com.aischool.webapp.family;

import java.io.File;

import com.aischool.routes.RouteMapping;
import com.aischool.service.FileUploadService;
import com.aischool.utils.ImageUtil;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

@RouteMapping(url = "/front/family/fileup")
public class FileUpController extends BaseFamilyController {

	@Inject
	FileUploadService fileUploadService;
	
	public void upload() {
		JsonResult jr = fileUploadService.uploadFile(this);
		renderJson(jr.Dic);
	}
	
	public void upload_base64() {
		
		this.setAttr("filemodel", "base64");
		

		this.setAttr("zoom_type", "wh");
		this.setAttr("zoom_w", 100);
		this.setAttr("zoom_h", 100);
		
		JsonResult jr = fileUploadService.uploadFile(this);
		renderJson(jr.Dic);
	}
	
	public void getfile() {
		int w = getInt("w", 0);
		int h = getInt("h", 0);
		String fname = get("file");
		if (StrKit.notBlank(fname)) {
			String filename1 = PathKit.getWebRootPath() + fname;
			if (w>0 && h>0) {
				int idx = filename1.lastIndexOf('.');
				String filename2 = filename1.substring(0, idx) + "_" + w + "x" + h + filename1.substring(idx);
				File file2 = new File(filename2);
				if (!file2.exists()) {
					ImageUtil.makeZoomImage(new File(filename1), filename2, w, h);
				}
				if (file2.exists()) {
					renderFile(file2);
					return;
				}else {
					renderFile(new File(filename1));
					return;
				}
			}else {
				File file1 = new File(filename1);
				if (file1.exists()) {
					
					renderFile(file1);
					return;
				}
			}
		}
		renderText("null");
	}
}
